package controllers.acta;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.ActaService;
import controllers.AbstractController;
import domain.Acta;

@Controller
@RequestMapping("/acta")
public class ActaController extends AbstractController {

	// Services
	@Autowired
	private ActaService actaService;

	// Constructors

	public ActaController() {
		super();
	}

	// Listing methods

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list(@RequestParam int comunidadDeVecinosId) {
		ModelAndView result;
		Collection<Acta> actas;

		actas = actaService.findActasByComunidadDeVecinos(comunidadDeVecinosId);

		result = new ModelAndView("acta/list");
		result.addObject("requestURI", "acta/list.do");
		result.addObject("backURI", "/welcome/index.do");
		result.addObject("actas", actas);

		return result;
	}

	@RequestMapping(value = "/morososList", method = RequestMethod.GET)
	public ModelAndView morosoList(@RequestParam int comunidadId) {
		ModelAndView result;
		Collection<Acta> actas;

		actas = actaService.findActasMorosoByComunidadDeVecinos(comunidadId);

		result = new ModelAndView("acta/morosoList");
		result.addObject("requestURI", "acta/morososList.do");
		result.addObject("backURI", "/welcome/index.do");
		result.addObject("actas", actas);

		return result;
	}

	// Edition

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int actaId) {
		ModelAndView result;
		Boolean editable;
		Acta acta;

		acta = actaService.findOne(actaId);

		editable = false;

		result = new ModelAndView("acta/edit");

		result.addObject("requestURI", "acta/edit.do");
		result.addObject("editable", editable);
		result.addObject("acta", acta);
		result.addObject("backURI", "/welcome/index.do");

		return result;

	}

}
